package zj.it.bhne.gridengineeringsurvey.myutils;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.ToastUtils;

import org.jetbrains.annotations.NotNull;

import java.io.File;

import zj.it.bhne.down.common.DownloadListener;
import zj.it.bhne.down.common.Downloader;
import zj.it.bhne.gridengineeringsurvey.R;

/**
 * 版本更新
 * Created by Jiang on 2019/3/21
 */
public class GetVersion {

    /**
     * 询问用户是否更新
     * @param updateUrl 更新地址
     */
    public static void update(String updateUrl, Context context) {
        View views = View.inflate(context, R.layout.layout_is_update, null);
        Button update = views.findViewById(R.id.up_btn_update);
        Button cancel = views.findViewById(R.id.up_btn_cancel);
        AlertDialog dialog = new AlertDialog.Builder(context).create();
        dialog.setView(views);
        dialog.show();
        update.setOnClickListener(v -> {
            dialog.dismiss();
            updateDown(updateUrl,context);
        });
        cancel.setOnClickListener(v -> dialog.dismiss());
    }

    /**
     * 下载apk
     * @param updateUrl 下载apk的网址
     */
    private static void updateDown(String updateUrl, Context context) {
        View views = View.inflate(context, R.layout.layout_update_down, null);
        Button update = views.findViewById(R.id.down_btn_update);
        ProgressBar pbProgress = views.findViewById(R.id.down_pbProgress);
        TextView tvProgress = views.findViewById(R.id.down_tvProgress);
        AlertDialog dialog = new AlertDialog.Builder(context).create();
        dialog.setView(views);
        dialog.show();
        File mImageFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
                + "/bhne/", "GridEngineering.apk");
        Downloader.INSTANCE.startDownload(updateUrl, mImageFile.getPath(), new DownloadListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onProgress(int percent) {
                pbProgress.setProgress(percent);
                tvProgress.setText("正在下载，请稍后。。。");
                update.setText("正在下载");
            }

            @Override
            public void onCompleted(@NotNull String filePath) {
                ToastUtils.showShort("下载完成");
                tvProgress.setText("下载完成");
                update.setText("安装");
            }

            @Override
            public void onFailure(@NotNull String errorMsg, @NotNull Throwable tr) {
                ToastUtils.showShort("下载失败");
                tvProgress.setText("下载失败");
            }
        });
        update.setOnClickListener(v -> {
            dialog.dismiss();
            if (tvProgress.getText().equals("下载完成")){
                AppUtils.installApp(mImageFile);//安装app
            }else {
                ToastUtils.showShort("请等待下载完成");
            }
        });
    }

}
